package com.hzjh.base.http.gson

import com.google.gson.Gson
import com.google.gson.TypeAdapter
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okio.Buffer
import retrofit2.Converter
import java.io.OutputStreamWriter
import java.nio.charset.Charset
/**
 * 返回的报文 json解析
 */
class GsonRequestBodyConverter <T>(val gson: Gson?, val adapter: TypeAdapter<T>?) :
    Converter<T, RequestBody> {

    private val MEDIA_TYPE: MediaType = "application/json; charset=UTF-8".toMediaType()
    private val UTF_8: Charset = Charset.forName("UTF-8")

    override fun convert(value: T): RequestBody? {
        val buffer = Buffer()
        val writer = OutputStreamWriter(buffer.outputStream(), UTF_8)
        val jsonWriter = gson?.newJsonWriter(writer)
        adapter?.write(jsonWriter, value)
        jsonWriter?.close()
        return buffer.readByteString().toRequestBody(MEDIA_TYPE)
    }
}